/**
 * 初始化文章管理详情对话框
 */
var ArticleInfoDlg = {
    articleInfoData : {},
    editor: null,
    validateFields: {
        title: {
            validators: {
                notEmpty: {
                    message: '文章标题不能为空'
                }
            }
        },
        content: {
            validators: {
                notEmpty: {
                    message: '文章内容不能为空'
                }
            }
        },
        catalogId: {
            validators: {
                notEmpty: {
                    message: '所在栏目不能为空'
                }
            }
        },
    }
};

/**
 * 清除数据
 */
ArticleInfoDlg.clearData = function() {
    this.articleInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ArticleInfoDlg.set = function(key, val) {
    this.articleInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ArticleInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ArticleInfoDlg.close = function() {
    parent.layer.close(window.parent.Article.layerIndex);
}

/**
 * 点击栏目input框时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
ArticleInfoDlg.onClickCatalog = function (e, treeId, treeNode) {
    $("#catalogSel").attr("value", instance.getSelectedVal());
    $("#catalogId").attr("value", treeNode.id);
};

/**
 * 显示栏目选择的树
 *
 * @returns
 */
ArticleInfoDlg.showCatalogSelectTree = function () {
    var cityObj = $("#catalogSel");
    var cityOffset = $("#catalogSel").offset();
    $("#catalogContent").css({
        left: cityOffset.left + "px",
        top: cityOffset.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 显示文章详情栏目选择的树
 *
 * @returns
 */
ArticleInfoDlg.showInfoCatalogSelectTree = function () {
    var cityObj = $("#catalogSel");
    var cityPosition = $("#catalogSel").position();
    $("#catalogContent").css({
        left: cityPosition.left + "px",
        top: cityPosition.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 隐藏部门选择的树
 */
ArticleInfoDlg.hideCatalogSelectTree = function () {
    $("#catalogContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

/**
 * 收集数据
 */
ArticleInfoDlg.collectData = function() {
    this.articleInfoData['content'] = ArticleInfoDlg.editor.txt.html();
    this.set('id').set('title').set('sort').set('status').set('catalogId');
}

/**
 * 验证数据是否为空
 */
ArticleInfoDlg.validate = function () {
    $('#articleInfoForm').data("bootstrapValidator").resetForm();
    $('#articleInfoForm').bootstrapValidator('validate');
    return $("#articleInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
ArticleInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/article/add", function(data){
        Feng.success("添加成功!");
        window.parent.Article.table.refresh();
        ArticleInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.articleInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ArticleInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/article/update", function(data){
        Feng.success("修改成功!");
        window.parent.Article.table.refresh();
        ArticleInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.articleInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "catalogBtn" || event.target.id == "catalogContent" || $(
            event.target).parents("#catalogContent").length > 0)) {
        ArticleInfoDlg.hideCatalogSelectTree();
    }
}

$(function() {
    Feng.initValidator("articleInfoForm", ArticleInfoDlg.validateFields);

    var ztree = new $ZTree("treeDemo", "/catalog/tree");
    ztree.bindOnClick(ArticleInfoDlg.onClickCatalog);
    ztree.init();
    instance = ztree;

    if($("#statusVal").val() == undefined){
        $("#status").val(0);
    }else{
        $("#status").val($("#statusVal").val());
    }
    //初始化编辑器
    var E = window.wangEditor;
    var editor = new E('#editor');
    editor.create();
    editor.txt.html($("#contentVal").html());
    ArticleInfoDlg.editor = editor;
});
